<?php 
/**
 * Класс, который реализовует базовые методы менеджемна даних в таблице
 *
 */

class FRM_View_Ext_Form2 extends FRM_View_Ext_Abstract
{
	/**
	 * Enter description here...
	 *
	 * @var array
	 */
	protected $baseClass = "x-plain";

	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	protected $labelWidth = 75;

	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	protected $labelAlign  = 'left';

	/**
	 * Enter description here...
	 *
	 * @var boolean
	 */
	protected $border = 'false';

	/**
	 * Enter description here...
	 *
	 * @var array
	 */
	protected $reader;

	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	protected $buttonAlign = 'right';

	/**
	 * Enter description here...
	 *
	 * @var PhpExt_Component
	 */
	protected $component;


	/**
	 * Enter description here...
	 *
	 * @param int $labelWidth
	 */
	public function setLabelWidth($labelWidth){
		$this->labelWidth = $labelWidth;
		return $this;
	}

	/**
	 * Enter description here...
	 *
	 * @param string $labelAlign
	 */
	public function setLabelAlign($labelAlign){
		$this->labelAlign = $labelAlign;
		return $this;
	}

	/**
	 * Enter description here...
	 *
	 * @param string $buttonAlign
	 */
	public function setButtonAlign($buttonAlign){
		$this->buttonAlign = $buttonAlign;
		return $this;
	}

	/**
	 * Enter description here...
	 *
	 * @param boolean $border
	 */
	public function setBorder($border){
		$this->border = $border;
		return $this;
	}


	/**
	 * Enter description here...
	 *
	 * @param array $reader
	 */
	public function setReader(array $reader){
		$this->reader = $reader;
		return $this;
	}

	/**
	 * Enter description here...
	 *
	 * @param string $reader
	 */
	public function addReader( $value){
		$this->reader[] = $value;
		return $this;
	}


	public function getComponent(){
		return $this->component;
		return $this;
	}

	public function render(){
		//creating reader
		$reader = new PhpExt_Data_JsonReader();
		$reader ->setSuccessProperty("success");
		$reader ->setRoot("data");
		
		//adding field to reader
		foreach ($this->reader as $v)
		{
			if (isset($reader_field))
			{
				unset($reader_field);
			}
			$name=	(isset($v['name']))?$v['name']:"";
			$map=	(isset($v['map']))?$v['map']:$name;
			$reader_field = new PhpExt_Data_FieldConfigObject($name, $map);
			$reader ->addField($reader_field);
		}
				
		// creating form
		$form = new PhpExt_Form_FormPanel();
		$form 	-> setLabelWidth($this->labelWidth)
				-> setReader($reader)
				-> setAutoHeight($this->autoHeight)
				-> setLabelAlign($this->labelAlign)
				-> setBorder($this->border)
				-> setButtonAlign($this->buttonAlign);

		$columnPanel = new PhpExt_Panel();
		// using ColumnLayout
		$columnPanel->setBorder(false)
		             ->setLayout(new PhpExt_Layout_ColumnLayout());
		$form->addItem($columnPanel);

		//- First column
		$firstColumn = new PhpExt_Panel();
		// Use FormLayout to enable field labels and autoarrange fields on the panel
		$firstColumn->setBorder(false)
		             ->setLayout(new PhpExt_Layout_FormLayout());
		             
		//- Second column
		$secondColumn = new PhpExt_Panel();
		// Use FormLayout to enable field labels and autoarrange fields on the panel
		$secondColumn->setBorder(false)
		             ->setLayout(new PhpExt_Layout_FormLayout());

		// adds the panel as a 50% column using ColumnLayoutData           
		$columnPanel->addItem($firstColumn, new PhpExt_Layout_ColumnLayoutData(0.5)); 
		$columnPanel->addItem($secondColumn, new PhpExt_Layout_ColumnLayoutData(0.5)); 
		             
				
		foreach ($this->items as $item)
		{
			$type=	(isset($item['type']))?$item['type']:"";
			$anchor=(isset($item['anchor']))?$item['anchor']:"100%";
			$name=	(isset($item['name']))?$item['name']:'';
			$map=	(isset($item['map']))?$item['map']:$name;
			$id=	(isset($item['id']))?$item['id']:$name;
			$label=	(isset($item['label']))?$item['label']:$name;
			$vtype=	(isset($item['vtype']))?$item['vtype']:null;
			$width=	(isset($item['width']))?$item['width']:null;
			$allowBlank=(isset($item['allowBlank']))?$item['allowBlank']:true;

			$itemEl = null;

			switch ($type) {
				case "TextField":
					$itemEl = PhpExt_Form_TextField::createTextField($name,$label,$id,$vtype);
					$itemEl->setWidth($width);
					$itemEl->setAllowBlank($allowBlank);
					$itemEl->setAnchor($anchor);
					break;
				case "Hidden":
					$itemEl = PhpExt_Form_Hidden::createHidden($name,null,$id);
					$itemEl->setWidth($width);
					//$item->setAllowBlank($allowBlank);
					break;

				default:
					break;
			}
			if(!is_null($itemEl)){
				$firstColumn -> AddItem($itemEl);
				$secondColumn -> AddItem($itemEl);
			}
		}

		// Ext.OnReady -----------------------
		PhpExt_Ext::addOnReady(
		$form->getJavascript(false,$this->varName)
		);
		$this->component = $form;
	}

	public function __construct()
	{
		$this->autoHeight = true;
		$this->layout = new PhpExt_Layout_FitLayout();
	}
}